package com.fuled.test.tool_noctice;
import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;

import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author hcy
 * 通知类
 */
public class NoticeEntity{

    public static String subject = "来自工具模块的通知";

    public static String contentFormat = "text/html;charset=UTF-8";

    public static boolean enableTimeoutNotice = false;

    public static boolean NoticeCondition() {
        return false;
    }

    public static<T> void sendEmail(String HOST, String PORT, String USERNAME, String PASSWORD, String toAddress, T message) throws MessagingException {
        Properties properties = new Properties();
        properties.put("mail.smtp.host", HOST);
        properties.put("mail.smtp.port", PORT);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        Authenticator auth = new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(USERNAME, PASSWORD);
            }
        } ;
        Session session = Session.getInstance(properties, auth);
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(USERNAME));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
        msg.setSubject(subject);
        msg.setContent(
                "<div style='text-align: center; padding: 20px; background-color: #f8d7da; border: 1px solid #f5c6cb; border-radius: 5px;'>" +
                        "<h1 style='color: #721c24; font-family: Arial, sans-serif;'>" +
                        "出现问题了！" +
                        "</h1>" +
                        "<p style='font-size: 16px; color: #856404;'>" +
                        message +
                        "</p>" +
                        "</div>",
                contentFormat
        );

        Transport.send(msg);
    }
}
